package com.rongc.client.freight.business.mine.view.activity;

import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.error.VolleyError;
import com.rongc.client.core.exception.RegexException;
import com.rongc.client.core.network.code.HandleCode;
import com.rongc.client.core.network.http.RequestManager;
import com.rongc.client.core.utils.ActivityUtils;
import com.rongc.client.core.utils.LogUtils;
import com.rongc.client.core.utils.RegexUtil;
import com.rongc.client.core.utils.StringUtils;
import com.rongc.client.freight.R;
import com.rongc.client.freight.UniApplication;
import com.rongc.client.freight.base.BaseActivity;
import com.rongc.client.freight.base.request.result.NullResult;
import com.rongc.client.freight.base.view.activity.WebActivity;
import com.rongc.client.freight.business.mine.request.api.BindCardApi;
import com.rongc.client.freight.utils.UtilMethod;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by ccqx on 2017/7/17.
 */

public class BankAddActivity extends BaseActivity implements View.OnClickListener{
    @Bind(R.id.et_name)
    EditText mEtName;
    @Bind(R.id.et_bank)
    EditText mEtBank;
    @Bind(R.id.et_type)
    EditText mEtType;
    @Bind(R.id.et_mobile)
    EditText mEtMobile;
    @Bind(R.id.tv_bank_tip)
    TextView mTvBankTip;

    Response.Listener<NullResult> respOrderListener = new Response.Listener<NullResult>() {

        @Override
        public void onResponse(NullResult response) {
            if (HandleCode.requestSuccess()) {
                UniApplication.getInstance().getUserInfo().setBanktype(mEtType.getText().toString());
                UniApplication.getInstance().getUserInfo().setBankcard(mEtBank.getText().toString());
                UniApplication.getInstance().getUserInfo().setBankphone(mEtMobile.getText().toString());
                UniApplication.getInstance().getUserInfo().setRealname(mEtName.getText().toString());
                UniApplication.getInstance().getUserInfo().save();
                ActivityUtils.toast("绑定银行卡成功。");
                finish();
            } else {
                UtilMethod.dismissProgressDialog(BankAddActivity.this);
            }
        }
    };

    Response.ErrorListener errorListener = new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            LogUtils.i("error.getMessage(): " + error.getMessage());
            UtilMethod.dismissProgressDialog(BankAddActivity.this);
        }
    };

    @OnClick({R.id.btn_submit,R.id.tv_bank_tip})
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_submit:
                try{
                    prepare();
                    RequestManager.getInstance().call(new BindCardApi(new BindCardApi.BindCardParams(
                            mEtType.getText().toString(),mEtBank.getText().toString(),
                            mEtMobile.getText().toString(),mEtName.getText().toString()),
                            respOrderListener, errorListener));
                }catch (RegexException e){
                    ActivityUtils.toast(e.getMessage());
                }
                break;
            case R.id.tv_bank_tip:
                Bundle bundle = new Bundle();
                bundle.putString("type","pay");
                ActivityUtils.startActivity(this,WebActivity.class,bundle);
                break;
        }
    }

    void prepare() throws RegexException {
        if (!RegexUtil.checkNotNull(mEtName.getText().toString())) {
            throw new RegexException("请输入姓名");
        } else if (!RegexUtil.checkNotNull(mEtBank.getText().toString())) {
            throw new RegexException("请输入银行卡号");
        }else if (!RegexUtil.checkNotNull(mEtType.getText().toString())) {
            throw new RegexException("请输入银行卡类型");
        }else if (!RegexUtil.checkNotNull(mEtMobile.getText().toString())) {
            throw new RegexException("请输入手机号");
        }else if (!RegexUtil.isPhone(mEtMobile.getText().toString())) {
            throw new RegexException("请输入正确手机号");
        }
    }

    @Override
    public void initView() {
        initToolbar(R.string.bank_title);
        mTvBankTip.setText(Html.fromHtml(String.format(getResources().getString(R.string.bank_protocol_tip))));

        if(StringUtils.isNotEmpty(UniApplication.getInstance().getUserInfo().getBankcard())) {
            mEtName.setText(UniApplication.getInstance().getUserInfo().getRealname());
            mEtBank.setText(UniApplication.getInstance().getUserInfo().getBankcard());
            mEtType.setText(UniApplication.getInstance().getUserInfo().getBanktype());
            mEtMobile.setText(UniApplication.getInstance().getUserInfo().getBankphone());
        }
    }


    @Override
    public int getLayoutId() {
        return R.layout.activity_addbank;
    }
}
